<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">


</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">


                <el-input placeholder="菜单名称" v-model.trim="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage = 1; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table
                    class="tree-table"
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    lazy
                    :load="load"
                    :tree-props="{children: 'children'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="访问路径"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="path"
                        label="路径级别">
                </el-table-column>

                <el-table-column
                        prop="parentMenuId"
                        label="自关联id">
                </el-table-column>
                <el-table-column
                        prop="level"
                        label="菜单级别">
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model.trim="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model.trim="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model.trim="formData.path"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model.trim="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-row>
                                <el-col :span="12">
                                    <el-form-item label="自关联Id" prop="parentMenuId">
                                        <div class="block">
                                            <el-cascader
                                                    v-model.trim="formData.parentMenuId"
                                                    @click="editParentMenuId()"
                                                    ref="cascader"
                                                    :props="{ checkStrictly: true }"
                                                    :options="options"
                                                    filterable
                                                    clearable
                                                    @change="change">
                                            </el-cascader>
                                        </div>
                                    </el-form-item>
                                </el-col>

                                <el-col :span="12">
                                    <el-form-item label="菜单级别" prop="level">
                                        <el-select v-model.trim="formData.level" placeholder="请选择">
                                            <el-option
                                                    v-for="level in levels"
                                                    :key="level"
                                                    :label="level"
                                                    :value="level">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model.trim="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="24">
                                <el-form-item label="注意事项" prop="description">
                                    <el-input v-model.trim="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model.trim="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model.trim="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="路径级别" prop="path">
                                    <el-input v-model.trim="formData.path" :disabled="true"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model.trim="formData.priority" :disabled="true"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-row>

                                <el-col :span="12">
                                    <el-form-item label="自关联Id" prop="placeholder">
                                        <div class="block">
                                            <el-cascader
                                                    v-model.trim="formData.parentMenuId"
                                                    @click="editParentMenuId()"
                                                    ref="cascader"
                                                    :props="{ checkStrictly: true }"
                                                    :options="options"
                                                    :disabled="true"
                                                    filterable
                                                    clearable
                                                    @change="change"
                                            ></el-cascader>
                                        </div>


                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="菜单级别" prop="level">
                                        <el-select v-model.trim="formData.level" placeholder="请选择" :disabled="true">
                                            <el-option
                                                    v-for="level in levels"
                                                    :key="level"
                                                    :label="level"
                                                    :value="level">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model.trim="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="24">
                                <el-form-item label="注意事项" prop="description">
                                    <el-input v-model.trim="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<!-- 引入组件库 -->
<script src="../plugins/elementui/index.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            id: 999999,
            menuIDList: [],
            pagination: {currentPage: 1, pageSize: 10, total: 0, queryString: null},
            dataList: [],
            formData: {},
            dialogFormVisible: false,
            dialogFormVisible4Edit: false,
            rules: {
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                path: [{required: true, message: '路径级别为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '优先级必填项', trigger: 'blur'}],
                level: [{required: true, message: '菜单级别为必填项', trigger: 'change'}],
                parentMenuId: [{required: true, message: '自关联Id别为必填项', trigger: 'change'}]
            },
            options: [],
            levels: [1, 2]
        },
        created() {
            this.findPage();
        },
        methods: {
            change(val) {
                let nodesObj = this.$refs['cascader'].getCheckedNodes();
                if (nodesObj[0].value == null) {
                    this.id = 9999;
                    console.log(this.id);
                } else {
                    this.id = nodesObj[0].value;
                    console.log(this.id);
                }
            },
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            },
            handleEdit() {
                if (this.formData.parentMenuId) {
                    if (this.formData.parentMenuId.length == 1) {
                        this.formData.parentMenuId = this.formData.parentMenuId[0]
                    }
                    if (this.formData.parentMenuId.length == 2) {
                        this.formData.parentMenuId = this.formData.parentMenuId[1]
                    }
                    if (this.formData.parentMenuId.length == 3) {
                        this.formData.parentMenuId = this.formData.parentMenuId[2]
                    }
                    if (this.formData.parentMenuId.length == 4) {
                        this.formData.parentMenuId = this.formData.parentMenuId[3]
                    }
                }
                let param = {
                    description: this.formData.description,
                    icon: this.formData.icon,
                    id: this.formData.id,
                    level: this.formData.level,
                    linkUrl: this.formData.linkUrl,
                    name: this.formData.name,
                    path: this.formData.path,
                    priority: this.formData.priority,
                    parentMenuId: this.formData.parentMenuId
                };
                /*todo 这个 parentMenuId 是不准改吗? 为何 param 中已经传了一遍，还要单独传*/
                axios.post("/menu/edit.do?id=" + this.id, param).then((resp) => {
                    if (resp.data.flag) {
                        this.$message({message: resp.data.message, type: "success"});
                    } else {
                        this.$message.error(resp.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                }).finally(() => {
                    this.dialogFormVisible4Edit = false;
                    this.findPage();
                });
            },
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid) => {
                        if (valid) {
                            this.formData.parentMenuId = null;
                            axios.post("/menu/add.do?id=" + this.id, this.formData).then((res) => {
                                this.dialogFormVisible = false;
                                if (res.data.flag) {
                                    this.findPage();
                                    this.$message({message: res.data.message, type: 'success'});
                                } else {
                                    this.$message.error(res.data.message);
                                }
                            })
                        }
                    }
                );
            },
            findPage() {
                let param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/menu/findPage.do", param).then((resp) => {
                    this.pagination.total = resp.data.total;
                    this.dataList = resp.data.rows;
                }).catch((r) => {
                    this.showMessage(r);
                });
            },
            resetForm() {
                this.formData = {};
                this.options = [];
                this.menuIDList = [];
                this.$refs.cascader.$refs.panel.clearCheckedNodes();
                this.$refs.cascader.$refs.panel.activePath = [];
            },
            handleCreate() {
                axios.post("/menu/findMenuparentMenuId.do").then((resp) => {
                    if (resp.data.flag) {
                        this.options = resp.data.data;
                        console.log(this.options);
                    } else {
                        this.$message.error(resp.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                });
                this.dialogFormVisible = true;
                this.resetForm();
                if (this.$refs.dataAddForm) {
                    this.$refs.dataAddForm.resetFields();
                }

            },
            handleUpdate(row) {
                if (this.$refs.dataEditForm) {
                    this.$refs.dataEditForm.resetFields();
                }
                this.dialogFormVisible4Edit = true;
                axios.get("/menu/findById.do?id=" + row.id).then((resp) => {
                    if (resp.data.flag) {
                        this.formData = resp.data.data;
                        axios.post("/menu/findMenuIdAndDisabled.do?id=" + row.id).then((resp) => {
                            if (resp.data.flag) {
                                this.options = resp.data.data;
                            } else {
                                this.$message.error(resp.data.message);
                            }
                        }).catch((r) => {
                            this.showMessage(r);
                        });
                    } else {
                        this.message.error(resp.data.message);
                    }
                });
            },
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            handleDelete(row) {
                this.$confirm("你确定要删除" + row.name + "的全部信息吗？", "提示", {type: 'warnig'}).then(() => {
                    axios.get("/menu/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.$message({type: 'success', message: res.data.message});
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    });
                }).catch(() => {
                    this.$message({type: 'info', message: '操作已取消'});
                });
            }
        },
        watch: {
            menuIDList() {
                if (this.$refs.cascader) {
                    this.$refs.cascader.dropDownVisible = false
                }
            }, formData() {
                if (this.$refs.cascader) {
                    this.$refs.cascader.dropDownVisible = false
                }
            }
        }
    })
</script>
</html>